package br.com.academia.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.academia.beans.Exercicio;

public class ExercicioDAO {
	private Connection connection;
	
	public ExercicioDAO(Connection connection) {
		this.connection = connection;
	}
	
	public List<String[]> consultaExercicios(List<String> dadosConsulta)  {
		List<String[]> retorno = new ArrayList<String[]>();
		String query = "SELECT exercicios.*, Alunos.nome, Alunos.sobreNome FROM exercicios " +
				" Inner Join Treinos on exercicios.Treinos_idTreino = Treinos.idTreino " +
				" Inner Join Alunos on Treinos.Aluno_idAluno = Alunos.idAluno";
		 try {
			 PreparedStatement stm = connection.prepareStatement(query);
			 ResultSet rs = stm.executeQuery();
			 while(rs.next()) {
				 String[] row = new String[dadosConsulta.size()];
				 for(int i = 0; i < row.length; i++) {
					 row[i] = rs.getString(dadosConsulta.get(i));
				 }
				 retorno.add(row);
			 }
		 } catch(SQLException e) {
			 e.printStackTrace();
		 }
		return retorno;
	}
	
	public Exercicio consultaExercicioId(int idExercicio) {
		Exercicio exercicio = new Exercicio();
		String query = "Select * from Exercicios where idExercicio = ?";
		try {
			PreparedStatement stm = connection.prepareStatement(query);
			stm.setInt(1, idExercicio);
			ResultSet rs = stm.executeQuery();
			
			if(rs.next()) {
				exercicio.setIdExercicio(rs.getInt("idExercicio"));
				exercicio.setNomeExercicio(rs.getString("nomeExercicio"));
				exercicio.setTreinos_idTreino(rs.getInt("Treinos_idTreino"));
				exercicio.setDetalheExercicio(rs.getString("detalheExercicio"));
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
		return exercicio;
	}
	
	public List<Exercicio> buscaExercicioSelecao() {
		List<Exercicio> listaExercicio = new ArrayList<Exercicio>();
		String query = "Select idExercicio, titulo from Exercicios";

		try {
			PreparedStatement stm = connection.prepareStatement(query);
			ResultSet rs = stm.executeQuery();

			while (rs.next()) {
				Exercicio exercicio = new Exercicio();
				exercicio.setIdExercicio(rs.getInt("idExercicio"));
				exercicio.setNomeExercicio(rs.getString("nomeExercicio"));
				exercicio.setTreinos_idTreino(rs.getInt("Treinos_idTreino"));
				listaExercicio.add(exercicio);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return listaExercicio;
	}
	
}
